package com.adinnet.admin.common.shiro;

import com.adinnet.admin.system.model.SysUser;
import com.adinnet.core.exception.BizException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * @author: sunjianbo
 * @date: 2019/5/20 15:25
 */
public class ShiroUtils {
    /**
     * 获取当前登录的user
     */
    public static SysUser getLoginUser() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            Object object = subject.getPrincipal();
            if (object != null) {
                return (SysUser) object;
            }
        }
        return null;
    }

    /**
     * 获取当前登录的userId
     */
    public static Integer getLoginUserId() {
        SysUser loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getId();
    }

    /**
     * 获取当前登录的username
     */
    public static String getLoginUserName() {
        SysUser loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getUserName();
    }
}
